package net.kazed.nextaction.context.activity;

import net.kazed.nextaction.ApplicationResource;
import net.kazed.nextaction.NextActionApplication;
import net.kazed.nextaction.R;
import net.kazed.nextaction.database.Task;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

/**
 * Abstract super class for started and due date based task lists.
 * @author Koert Zeilstra
 */
public abstract class AbstractDatedTasksActivity extends ListActivity {

    private static final int MENU_SELECT_DAY = Menu.FIRST;
    private static final int MENU_SELECT_WEEK = MENU_SELECT_DAY + 1;
    private static final int MENU_SELECT_MONTH = MENU_SELECT_WEEK + 1;

    private static final int VIEW_TASK = 1;

    private ApplicationResource applicationResource;

    @Override
    public void onCreate(Bundle icicle) {
        NextActionApplication application = (NextActionApplication) getApplication();
        applicationResource = application.getApplicationResource();

        setContentView(getContentViewId());
        setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);

        int numberOfDays = getNumberOfDaysPreference();
        createListCursor(applicationResource, numberOfDays);

        super.onCreate(icicle);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Uri itemUri = ContentUris.withAppendedId(Task.CONTENT_URI, id);
        Intent intent = new Intent(Intent.ACTION_VIEW, itemUri);
        startActivityForResult(intent, VIEW_TASK);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Resources resources = getResources();
        menu.add(0, MENU_SELECT_DAY, 0, resources.getString(R.string.menu_select_day)).setIcon(
                        android.R.drawable.ic_menu_day).setAlphabeticShortcut('d');
        menu.add(0, MENU_SELECT_WEEK, 0, resources.getString(R.string.menu_select_week)).setIcon(
                        android.R.drawable.ic_menu_week).setAlphabeticShortcut('w');
        menu.add(0, MENU_SELECT_MONTH, 0, resources.getString(R.string.menu_select_month)).setIcon(
                        android.R.drawable.ic_menu_month).setAlphabeticShortcut('m');
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int numberOfDays = 0;
        switch (item.getItemId()) {
        case MENU_SELECT_DAY:
            numberOfDays = TaskDatePeriod.DAY.getNumberOfDays();
            break;
        case MENU_SELECT_WEEK:
            numberOfDays = TaskDatePeriod.WEEK.getNumberOfDays();
            break;
        case MENU_SELECT_MONTH:
            numberOfDays = TaskDatePeriod.MONTH.getNumberOfDays();
            break;
        }
        saveNumberOfDaysPreference(numberOfDays);
        createListCursor(applicationResource, numberOfDays);

        return super.onOptionsItemSelected(item);
    }

    /**
     * @return ID of content view.
     */
    protected abstract int getContentViewId();

    /**
     * @return Number of days of display period from preference.
     */
    protected abstract int getNumberOfDaysPreference();

    /**
     * @param numberOfDays Number of days of display period to save in preference.
     */
    protected abstract void saveNumberOfDaysPreference(int numberOfDays);

    /**
     * Create cursor for list view.
     * @param numberOfDays Number of days for selection period of tasks.
     */
    protected abstract Cursor createListCursor(ApplicationResource applicationResource, int numberOfDays);

}
